#include <treaty_base.h>
treaty_base::treaty_base()
{
}
uint32_t treaty_base::msb_push_u8_to_array(vector<uint8_t> &vec, const uint8_t data)
{
    vec.push_back(data);
    return vec.size();
}
uint32_t treaty_base::msb_push_u16_to_array(vector<uint8_t> &vec, const uint16_t data)
{
    vec.push_back(data >> 8);
    vec.push_back(data & 0xFF);
    return vec.size();
}

uint32_t treaty_base::lsb_push_u16_to_array(vector<uint8_t> &vec, const uint16_t data)
{

    vec.push_back(data & 0xFF);
    vec.push_back(data >> 8);
    return vec.size();
}

uint32_t treaty_base::msb_push_u32_to_array(vector<uint8_t> &vec, const uint32_t data)
{
    vec.push_back(data >> 24);
    vec.push_back(data >> 16);
    vec.push_back(data >> 8);
    vec.push_back(data & 0xFF);
    return vec.size();
}
uint32_t treaty_base::msb_push_float_to_array(vector<uint8_t> &vec, const float data)
{
    uint8_t *p = (uint8_t *)&data;
    vec.push_back(p[3]);
    vec.push_back(p[2]);
    vec.push_back(p[1]);
    vec.push_back(p[0]);
    return vec.size();
}
uint32_t treaty_base::msb_push_array_to_array(vector<uint8_t> &vec, const uint8_t *data, uint16_t len)
{
    for (uint16_t i = 0; i < len; i++)
    {
        vec.push_back(data[i]);
    }
    return vec.size();
}

uint32_t treaty_base::create_treaty_header(vector<uint8_t> &vec, const uint16_t ev_id, vector<uint8_t> &data)
{
    vec.clear();
    msb_push_u16_to_array(vec, ev_id); // 事务处理标识符
    msb_push_u16_to_array(vec, 0);     // 协议标识符0=Modbus TCP

    msb_push_u16_to_array(vec, data.size()); // 数据域长度

    return vec.size();
}
